`
https://leetcode.cn/problems/maximum-average-subarray-i/
`

/**
 * @param {number[]} nums
 * @param {number} k
 * @return {number}
 */
var findMaxAverage = function (nums, k) {
  const n = nums.length
  let sum = 0
  let maxAve = -Infinity
  let left = 0, right = 0

  // 维护一个长度为 k 的滑动窗口即可
  while (right < n) {
    const n1 = nums[right++]
    sum += n1
    const len = right - left

    if (len >= k) {
      // 在长度为 k 的时候再更新答案
      maxAve = Math.max(maxAve, sum / len)
      const n2 = nums[left++]
      sum -= n2
    }
  }

  return maxAve
};